import docx
import os
import re


class GetImg():

    def __init__(self):
        pass


    def get_word_pictures(self,word_path, result_path):
        """
        提取word文档内的图片
        :param word_path: word文件
        :param result_path: 结果目录
        :return:
        """
        doc = docx.Document(word_path)
        dict_rel = doc.part._rels
        for rel in dict_rel:
            rel = dict_rel[rel]
            if "image" in rel.target_ref:
                if not os.path.exists(result_path):
                    os.makedirs(result_path)
                img_name = re.findall("/(.*)", rel.target_ref)[0]
                img_name = f'{img_name}'
                with open(f'{result_path}/{img_name}', "wb") as f:
                    f.write(rel.target_part.blob)


# if __name__ == '__main__':
#     run = get_img()
#     run.get_word_pictures("../wordfiles/1_E-label Guide for Lenovo TB-J606F for ROW.docx"
#                        ,"../temp_img")

